package org.feidao.chapter62.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by linghufeixia on 2022-11-8.
 */
@Service
public class ProducerService {
    public static final String topic = "test";

    @Autowired
    private KafkaTemplate<?,String> kafkaTemplate;

    //向topic发送消息
    public void sendMessage(String message){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",message);
        kafkaTemplate.send(topic,jsonObject.toJSONString());
    }
}
